package com.epfresh.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.CartGood;
import com.epfresh.views.tag.FlowLayout;
import com.epfresh.views.tag.TagAdapter;
import com.epfresh.views.tag.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MerMsgDialog extends Dialog {


    EditText edInput;

    TagFlowLayout tagFlowLayout;

    TextView tvCancel;
    TextView tvSure;
    TextView tvCntNum;
    TextView tvFastMer;


    CartGood cartGood;
    TagAdapter<String> adapter;
    List<String> list = new ArrayList<>();
    OnMerMsgInputListener onMerMsgInputListener;

    public OnMerMsgInputListener getOnMerMsgInputListener() {
        return onMerMsgInputListener;
    }

    public void setOnMerMsgInputListener(OnMerMsgInputListener onMerMsgInputListener) {
        this.onMerMsgInputListener = onMerMsgInputListener;
    }

    public MerMsgDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);
//        setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {

//            }
//        });


        View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_mer_msg, null);

        tagFlowLayout = (TagFlowLayout) v.findViewById(R.id.tfl_tag);

        edInput = (EditText) v.findViewById(R.id.et_mer_msg);

        tvCancel = (TextView)  v.findViewById(R.id.tv_mer_msg_cancel);
        tvFastMer = (TextView)  v.findViewById(R.id.tv_mer_msg_tag);

        tvSure = (TextView)  v.findViewById(R.id.tv_mer_msg_sure);
        tvCntNum = (TextView)  v.findViewById(R.id.tv_count_num);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMerMsgInputListener != null && cartGood != null) {
                    onMerMsgInputListener.onMerMsgInputListener(edInput.getText().toString(), cartGood);
                }

            }
        });

        edInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                tvCntNum.setText(s.length()+"/30");
            }
        });



        adapter = new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.mer_msg_tag,
                        parent, false);
                tv.setText(s);
                return tv;
            }
        };
        tagFlowLayout.setAdapter(adapter);
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                if (position < list.size()){
                    String s = list.get(position);
                    if (s != null && !"".equals(s)){
                        String text = edInput.getText().toString();
                        if (text.length() > 0){
                            edInput.setText(text +" "+ s);
                        }else {
                            edInput.setText(s);
                        }

                        edInput.setSelection(edInput.length());
                    }
                }
                return true;
            }
        });

        edInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
        setContentView(v);


    }

    public void show(CartGood cartGood,List<String> list) {
        this.cartGood = cartGood;
        if (cartGood != null) {
            String merMsg = cartGood.getMerMsg();
            edInput.setText(merMsg);
            edInput.setSelection(edInput.length());
            this.list.clear();
            if (list != null && list.size() > 0){
                this.list.addAll(list);
            }
            adapter.notifyDataChanged();

            if (this.list.size() == 0){
                tvFastMer.setVisibility(View.INVISIBLE);
            }else {
                tvFastMer.setVisibility(View.VISIBLE);
            }
        }
        super.show();




    }

    public interface OnMerMsgInputListener {
        public void onMerMsgInputListener(String msg, CartGood cartGood);
    }


}